<script>
    jxt.list.newJson = function(dataJson, attrs) {
        if (!dataJson) dataJson = jxt.list.dataJson;
        var ret = [];
        var _self = jxt.tag.newJson(dataJson);
        _self.children = [];
        var row;
        if (attrs) {
            row = attrs.row;
        }
        if (!row) row = dataJson.init.rowcount;
        for (var i = 0; i < row; i++) {
            var _text = jxt.text.newJson();
            ret.push(_text);
            _self.children.push([_text.id]);
        }
        ret.splice(0, 0, _self);
        return ret;
    };
    jxt.list.help_callback= jxt.list._add_ = function (subscribe_id, eventkey, attrs, event_source_id) {

        switch (eventkey){
            case "help_img":
                jxt.list.add(subscribe_id, "img", attrs);
                break;
            case "help_file":
                jxt.list.add(subscribe_id, "file", attrs);
                break;
            case "help_link":
                jxt.list.add(subscribe_id, "link", attrs);
                break;
        }
    };
    jxt.list.add = function(eleid, tagname,attrs) {

        var _eleid = eleid;
        var node = jxt.getnode(_eleid);
        var newnode = jxt.newJson(tagname,attrs);
        var at_id = jxt.tag.getNowEditEle(eleid);
        node = jxt.list._add(node, newnode, at_id);

        jxt.msg.radio("list", "addchildnode", {"newnodelist": newnode});
        jxt.msg.radio(eleid, "nodechange");
        jxt.render(eleid, true);
    };

    jxt.list._add = function(node, newnode, at_id) {
        if (!node.children) {
            node.children = [];
            node.children.push([newnode.id]);
            return node;
        }
        var b = false;
        if (at_id) {
            var rows = node.children;
            for (var i = 0; i < rows.length; i++) {
                var li = rows[i];
                for (var j = 0; j < li.length; j++) {
                    if (at_id == li[j]) {
                        node.children[i].splice(j + 1, 0, newnode.id);
                        b = true;
                        break;
                    }
                }
            }
        }
        if (!b) {
            node.children[0].push(newnode.id);
        }
        return node;

    };
    jxt.list.addrow = function(eleid) {
        var n = jxt.getnode(eleid);
        var at_id =jxt.tag.getNowEditEle(eleid);
        var b = false;
        var ii = -1;
        if (at_id) {
            var rows = n.children;
            for (var i = 0; i < rows.length; i++) {
                var li = rows[i];
                for (var j = 0; j < li.length; j++) {
                    if (at_id == li[j]) {
                        ii = i;
                        b = true;
                        break;
                    }
                }
                if (b) break;
            }
        }
        if (ii == -1) {
            ii = n.children.length - 1;
        }
        var _text = jxt.newJson("text");
        jxt.addNode(_text);

        n.children.splice(ii + 1, 0, [_text.id]);
        jxt.msg.radio(eleid, "nodechange");
        jxt.render(eleid, true);
    };

    jxt.list.delrow = function(eleid) {
        var node = jxt.getnode(eleid);
        if (!confirm("del?")) return;
        var at_id = jxt.tag.getNowEditEle(eleid);
        var b = false;
        var ii = -1;
        if (at_id) {
            var rows = node.children;
            for (var i = 0; i < rows.length; i++) {
                var li = rows[i];
                for (var j = 0; j < li.length; j++) {
                    if (at_id == li[j]) {
                        ii = i;
                        b = true;
                        break;
                    }
                }
                if (b) break;
            }
        }
        if (ii > -1) {
            node.children.splice(ii, 1);
            jxt.msg.radio(eleid, "nodechange");
            jxt.render(eleid, true);
        }

    };


    jxt.list.create = function (subscribe_id, eventkey, attrs, event_source_id) {
        var node = jxt.list.newJson();
        if(attrs && attrs.tpname)
        {
            node[0].tpname=attrs.tpname;
        }
        console.log("list.editbegin.before  "+JSON.stringify(jxt.data));

        jxt.msg.radio("list", "addmainnode", {"newnodelist": node});

        jxt.route.exe('list.editbegin',null,node[0].id);
        console.log("list.editbegin.after  "+JSON.stringify(jxt.data));
    };
   /* jxt.list.help_callback=function (subscribe_id, eventkey, attrs, event_source_id) {
        jxt.list._add_(subscribe_id, eventkey, attrs, event_source_id);
    };*/
    jxt.msg.subscribe("list", "createlist", jxt.list.create);
</script>